import export
from google.cloud import speech_v1p1beta1 as speech
import os



os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your-service-account-file.json"
# 初始化客户端
client = speech.SpeechClient()

# 配置请求参数
config = speech.RecognitionConfig(
    encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code="de-DE",  # 识别德语
    enable_speaker_diarization=True,  # 启用扬声器分离
    diarization_speaker_count=2  # 预计的说话者数量
)

# 音频文件路径
audio = speech.RecognitionAudio(uri="gs://your-bucket/audio-file.wav")

# 调用 API 进行识别
response = client.recognize(config=config, audio=audio)

# 输出结果
for result in response.results:
    alternative = result.alternatives[0]
    print(f"Transcript: {alternative.transcript}")
    for word in alternative.words:
        print(f"Speaker {word.speaker_tag}: {word.word}")
